package com.jsmy.softapp.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jsmy.softapp.MyApp;
import com.jsmy.softapp.R;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.SharePresenter;
import com.jsmy.softapp.util.SPF;
import com.jsmy.softapp.util.ShareUtil;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.ShareView;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AR.A_SHARE)
public class ShareActivity extends BaseActivity<ShareView, SharePresenter> implements ShareView, UMShareListener {

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.m_webview)
    WebView mWebview;

    @Autowired
    public String id;
    @Autowired
    public String url;
    @Autowired
    public String title;
    @Autowired
    public String note;
    @Autowired
    public String share;

    String sharePt;
    @BindView(R.id.img_share)
    ImageView imgShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_share;
    }

    @Override
    protected SharePresenter createPresenter() {
        return new SharePresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareActivity.this.finish();
            }
        });

        tvContent.setText(title);

        imgShare.setVisibility(View.VISIBLE);
        imgShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (share.equals("Y")){
                    ShareUtil.shareWeb(ShareActivity.this, url, title, note, ShareActivity.this);
                }else {
                    ShareUtil.shareWeb(ShareActivity.this, url + SPF.getString(MyApp.getMyApp(), SPF.SP_ZH, MyApp.getMyApp().bean.getUsername()), title, note, ShareActivity.this);
                }

            }
        });

//        if (share.equals("Y")) {
//            imgShare.setVisibility(View.VISIBLE);
//            imgShare.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ShareUtil.shareWeb(ShareActivity.this, url, title, note, ShareActivity.this);
//                }
//            });
//        } else {
//            imgShare.setVisibility(View.INVISIBLE);
//            imgShare.setOnClickListener(null);
//        }

        mWebview.setVerticalScrollbarOverlay(true); //指定的垂直滚动条有叠加样式
        WebSettings settings = mWebview.getSettings();
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放
        settings.setDomStorageEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebview.clearHistory();
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        if (share.equals("Y")){
            mWebview.loadUrl("" + url);
        }else {
            mWebview.loadUrl("" + url + SPF.getString(MyApp.getMyApp(), SPF.SP_ZH, MyApp.getMyApp().bean.getUsername()));
        }


    }

    @Override
    protected void initData() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.APP_SAVE_FX_INFO:
                ToastUtil.showShort(this, "分享成功！");
                break;
        }
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        presenter.savefxinfo("", id, sharePt, "");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，" + throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }
}
